<template>
  <!-- (医生门诊)今日病人 -->
  <section>
    <div class="form-tool">
      <div>
        <el-form :inline="true" @submit.native.prevent="getData(-1)">
          <el-form-item>
            <!-- <el-input placeholder="请输入姓名或手机号"></el-input> -->
            <patientSearch v-model="form.patientId"></patientSearch>
          </el-form-item>
          <el-form-item>
            <el-button icon="el-icon-search" type="primary" native-type="submit" circle></el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <el-row :gutter="20">
      <el-col
        v-for="(item,index) in tableList"
        :key="index"
        :xs="12"
        :sm="12"
        :md="12"
        :lg="8"
        :xl="6"
        class="reserve-user-list"
      >
        <div :class="['card-box',classToName(item.status)]">
          <div class="card-nr">
            <div>
              <img
                v-if="!item.patientInfo.head"
                src="@/img/unknown-avatar.png"
                class="head-img"
                alt
                style="width:70px;height:70px;"
              />
              <img
                v-else
                :src="item.patientInfo.head|imageUrl"
                class="head-img"
                alt
                style="width:70px;height:70px;"
              />
            </div>
            <div>
              <p class="overflow-omit">
                <span style="font-size:18px;">{{item.patientInfo.name}}</span>
                &nbsp;
                <span>
                  （{{item.patientInfo.sex|toSex}}&nbsp;
                  <span v-if="item.patientInfo.age>17">{{item.patientInfo.age}}岁</span>
                  <span v-else>{{item.patientInfo.birthday|toTimeContrast}}</span>）
                </span>
              </p>
              <div
                class="overflow-omit"
              >预约时间：{{item.dateDay}}&nbsp;{{item.startTime}}~{{item.endTime}}</div>
              <div
                class="overflow-omit"
              >预约：({{item.deptName}}、{{item.registerType|changeAssist(reserveTypeArray)}})</div>
              <div class="overflow-omit">预约内容：{{item.registerContent}}</div>
              <div class="overflow-omit">创建时间：{{item.gmtCreate}}</div>
            </div>
          </div>
          <div style="height:1px;"></div>
          <!-- <div style="height:150px;">123</div> -->
          <div class="card-footer">
            <div @click="onJieZhen(item)">接诊</div>
            <div @click="onReport(item)">查看报告</div>
            <div>
              <el-dropdown
                trigger="click"
                style="width:100%;"
                @command="onMoreOperations($event,item)"
              >
                <div>更多操作</div>
                <el-dropdown-menu slot="dropdown">
                  <el-dropdown-item command="1">
                    <i class="el-icon-s-custom vertical-middle"></i>
                    <span class="vertical-middle">患者信息</span>
                  </el-dropdown-item>
                  <!-- <el-dropdown-item command="2">
                    <i class="iconfont iconyuyue vertical-middle"></i>
                    <span class="vertical-middle">预约</span>
                  </el-dropdown-item>-->
                  <el-dropdown-item command="3">
                    <i class="el-icon-document-add vertical-middle"></i>
                    <span class="vertical-middle">修改接诊类型</span>
                  </el-dropdown-item>
                </el-dropdown-menu>
              </el-dropdown>
            </div>
          </div>
          <div v-if="item.patientInfo.levelId" class="vip-box">
            <el-tooltip :content="item.patientInfo.levelName" placement="top">
              <img src="@/img/icon-vip.png" alt />
            </el-tooltip>
          </div>
        </div>
      </el-col>

      <!-- <el-col v-for="(item,index) in 12" :key="index" :xs="12" :sm="12" :md="12" :lg="8" :xl="6" class="p-sm">
          <div style="background-color: red;">123456</div>
      </el-col>-->
    </el-row>
    <div v-if="tableList.length == 0" style="color:#999;padding:10px 0;text-align:center;">
      <i class="iconfont iconwushuju" style="font-size:40px;"></i>
      <div>暂无数据</div>
    </div>
    <br />
    <pager v-model="pager" @change-pagenum="getData"></pager>
    <!-- 修改接诊类型 -->
    <el-dialog
      title="修改接诊类型"
      :visible.sync="dialogJZLX"
      width="400px"
      custom-class="dialog-box"
      :close-on-click-modal="false"
      :append-to-body="true"
    >
      <el-select v-model="JZLX_value" value-key="id" clearable style="width:100%;">
        <el-option
          v-for="(item,index) in reserveTypeArray"
          :key="index"
          :value="item"
          :label="item.desc"
        ></el-option>
      </el-select>
      <span slot="footer">
        <el-button type="primary" :loading="btnLoading" @click="onJZLXSubmit">确 定</el-button>
      </span>
    </el-dialog>
    <dialog-report
      v-model="dialogReport"
      :cureId="curItem.cureId"
      :patientId="curItem.patientInfo.patientId"
    ></dialog-report>
  </section>
</template>
<script>
export default {
  props: ["doctorInfo"],
  components: {
    patientSearch: () => import("@/pages/_com/patientSearch.vue"),
    dialogReport: () => import("./dialogReportV2.vue")
  },
  data() {
    return {
      curItem: {
        patientInfo: {
          patientId: ""
        }
      },
      dialogReport: false,
      btnLoading: false,
      JZLX_value: null,
      dialogJZLX: false,
      reserveTypeArray: [],
      triageStatusArray: [],
      tableList: [],
      pager: { pageSize: 12, pageNum: 1, total: 0 },
      form: {
        patientId: "",
        dateDayStart: "",
        dateDayEnd: "",
        deptId: "",
        doctorId: "",
        status: "2,5,6"
      }
    };
  },
  async created() {
    let th = this;
    let date = new Date();
    th.form.dateDayStart = th.$utils.FormatTime("yyyy-MM-dd", date.getTime());
    th.form.dateDayEnd = th.$utils.FormatTime("yyyy-MM-dd", date.getTime());
    th.showLoading();
    th.triageStatusArray = await th.getCommonConfigByKey("预约状态");
    th.reserveTypeArray = await th.getCommonConfigByKey("预约类型");
    let t = setInterval(() => {
      if (Object.keys(th.doctorInfo).length > 0) {
        clearInterval(t);
        th.form.doctorId = th.doctorInfo.id;
        th.getData();
      }
    }, 300);
  },
  mounted() {},
  methods: {
    //查看报告
    onReport(obj) {
      // if (!obj.cureId) {
      //   th.$message.warning("该患者还未有就诊记录");
      //   return;
      // }
      this.curItem = obj;
      this.dialogReport = true;
    },
    getData(type) {
      let th = this;
      if (type == -1) {
        th.pager.pageNum = 1;
        th.pager.total = 0;
      }
      th.showLoading();
      let j = {};
      Object.assign(j, th.form, th.pager);
      th.$api.getRegisterListInfo(j).then(res => {
        if (res.executed) {
          th.tableList = res.dataBody.list;
          th.pager.total = res.dataBody.total;
        }
        th.showLoading(false);
      });
    },
    //接诊
    onJieZhen(obj) {
      let th = this;
      th.$confirm(`确认开始接诊${obj.patientInfo.name}吗`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(() => {
          fn();
        })
        .catch(() => {});

      const fn = function() {
        let j = {
          patientId: obj.patientInfo.patientId,
          doctorId: obj.doctorId,
          deptId: obj.deptId,
          registerId: obj.id,
          isFirstVisit: obj.registerType,
          id: obj.cureId ? obj.cureId : null
        };
        th.showLoading();
        th.$api.doctorJieZhen(j).then(res => {
          th.showLoading(false);
          if (res.executed) {
            let url = "/page/jieZhen";
            let query = {
              patientId: th.jiaMi(obj.patientInfo.patientId),
              cureId: th.jiaMi(obj.cureId ? obj.cureId : res.data),
              deptId: th.jiaMi(obj.deptId),
              doctorId: th.jiaMi(obj.doctorId),
              isNew: j.id ? false : true,
              deptName: obj.deptName,
              doctorName: th.doctorInfo.trueName,
              professionalDeptName: th.doctorInfo.professionalDeptName
            };
            // th.addNavMenu(
            //   `患者：${obj.patientInfo.name}`,
            //   `${url}?${th.$utils.JsonToUrl(query)}`
            // );
            th.$router.push({
              path: url,
              query: query
            });
          } else {
            th.$message.error(res.message);
          }
        });
      };
    },
    //修改就诊类型
    onJZLXSubmit() {
      let th = this;
      let j = {
        id: th.curItem.id,
        registerType: th.JZLX_value.id,
        registerName: th.JZLX_value.desc
      };
      th.btnLoading = true;
      th.$api.updateRegisterRecord(j).then(res => {
        if (res.executed) {
          th.$message.success("操作成功");
          th.dialogJZLX = false;
          th.curItem.registerType = th.JZLX_value.id;
        } else {
          th.$message.error(res.message);
        }
        th.btnLoading = false;
      });
    },
    //更多操作
    onMoreOperations(key, obj) {
      let th = this;
      let url = "";
      switch (key) {
        case "1":
          url = "/page/userDetail";
          let j = th.getUrlToken({
            id: th.jiaMi(obj.patientInfo.patientId)
          });
          th.addNavMenu("病人库", url + th.$utils.JsonToUrl(j),'userDetail');
          th.$router.push({
            path: url,
            query: j
          });
          break;
        case "2":
          url = "/page/reservecentre";
          //验证页面权限
          if (!th.verifyUrl(url)) {
            th.$message.warning("权限不足");
            return;
          }
          th.delCache("reservecentre");
          //修改预约
          th.addNavMenu("预约", url, "reservecentre");
          th.$router.push({
            path: "/page/reservecentre",
            query: th.getUrlToken({
              patientId: th.jiaMi(obj.patientInfo.patientId),
              name: obj.patientInfo.name,
              yyId: th.jiaMi(obj.id)
            })
          });
          break;
        case "3":
          th.curItem = obj;
          th.JZLX_value = {
            id: obj.registerType,
            desc: obj.registerName
          };
          th.dialogJZLX = true;
          break;
        case "4":
          break;
      }
    },
    //判断类名
    classToName(type) {
      switch (type) {
        case 1:
          return "bg-yjz";
          break;
        case 2:
          return "bg-sy";
          break;
        case 3:
          return "bg-hong";
          break;
        case 4:
          return "bg-yqx";
          break;
        case 5:
          return "bg-jzz";
          break;
        default:
          return "bg-dfz";
          break;
      }
    }
  }
};
</script>
<style lang="scss" scoped>
@import "../../_css/reserve.scss";
</style>
